package com.zxs.admin.service.impl;

import client.ProductClient;
import com.zxs.admin.service.AdminProducetService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import parm.PageParam;

import parm.ProductSaveParam;
import pojo.Product;
import utils.R;

/**
 * @author zxs
 * *9 22:25
 */
@Service
@Slf4j
public class AdminProducetServiceImpl implements AdminProducetService {

    @Autowired
    private ProductClient productClient;

    /**
     * 查询全部商品
     * @param pageParam
     * @return
     */
    @Override
    public R listPage(PageParam pageParam) {
        R products = productClient.getProducts(pageParam);
        log.info("products: " + products);
        return products;
    }

    /**
     * 修改商品
     * @param product
     * @return
     */
    @CacheEvict(value = "list.product",allEntries = true)
    @CachePut(value = "product",key = "#product.productId")
    @Override
    public R update(Product product) {
        R update = productClient.update(product);
        return update;
    }

    /**
     * 新增商品
     * @param productSaveParam
     * @return
     */
    @CacheEvict(value = "list.product", allEntries = true)
    @Override
    public R adminSave(ProductSaveParam productSaveParam) {
        R r = productClient.adminSave(productSaveParam);
        log.info("admin save",r);
        return r;
    }
    /**
     * 删除商品数据
     *    清空缓存
     *    调用商品服务
     * @param productId
     * @return
     */
    @Caching(
            evict = {
                    @CacheEvict(value = "list.product",allEntries = true),
                    @CacheEvict(value = "product",key = "#productId")
            })
    @Override
    public R remove(Integer productId) {
        R remove = productClient.remove(productId);
        return remove;
    }
}
